package com.ztjc.day02_storage.acivity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.ztjc.day02_storage.R;
import com.ztjc.day02_storage.util.SharedPerferencesUtil;

import java.util.Map;

/**
 * 使用SharedPerferences存储数据
 */
public class SharedPerferences extends Activity {

    // 声明控件
    private EditText etAccount, etPass;
    private CheckBox cbRemenber;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shared_perferences);

        etAccount = (EditText) findViewById(R.id.et_account);
        etPass = (EditText) findViewById(R.id.et_pass);
        cbRemenber = (CheckBox) findViewById(R.id.cb_remenber);

        getAccount();
    }

    /**
     * 获取账户信息回显
     */
    private void getAccount() {
        Map<String, String> account = SharedPerferencesUtil.getAccount(this);
        etAccount.setText(account.get("account"));
        etPass.setText(account.get("pass"));
    }

    /**
     * 登陆
     *
     * @param view
     */
    public void login(View view) {
        String account = etAccount.getText().toString();
        String pass = etPass.getText().toString();

        if (cbRemenber.isChecked()) {
            boolean b = SharedPerferencesUtil.saveAccount(this, account, pass);
            if (b) {
                Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
            }
        }
        Toast.makeText(this, "登陆成功", Toast.LENGTH_SHORT).show();
    }
}
